package com.joker.controller;

import com.joker.entity.SysDict;
import com.joker.entity.SysRoleMenu;
import io.gitee.guoguoleo.helper.JokerHelper;
import io.gitee.guoguoleo.wrapper.QueryWrapper;
import io.gitee.guoguoleo.wrapper.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@RestController
public class SysDictController {

    @Autowired
    private JokerHelper jokerHelper;

    @GetMapping(value = "get/{id}")
    public Object get(@PathVariable(value = "id") Long id) {
//        return this.jokerHelper.lambdaQuery(SysDict.class)
//                .like(SysDict::getDictKey, "user_type")
//                .orderByAsc(SysDict::getId)
//                .list();
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SysDict::getDictKey, "user_type")
                .exclude(SysDict::getDictKey,SysDict::getDictValue)
                .orderByAsc(SysDict::getId);
        return this.jokerHelper.getList(SysDict.class, queryWrapper);
    }

    @GetMapping(value = "get2")
    public Object get2() {
        return this.jokerHelper.lambdaQuery(SysRoleMenu.class)
                .list()
                ;
    }

    @GetMapping(value = "save")
    public String save() {

        SysDict sysDict = new SysDict()
                .setDictKey("user_type")
                .setDictSort(1)
                .setDictTitle("test");
        this.jokerHelper.lambdaUpdate(SysDict.class)
                .save(sysDict);

        return "";
    }


    @GetMapping(value = "saveList")
    public String saveList() {

        SysDict sysDict = new SysDict();
        sysDict.setDictKey("user_type")
                .setDictValue(1)
                .setDictSort(1)
                .setDictTitle("test");

        List<SysDict> sysDictList = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 10; i++) {
            sysDictList.add(sysDict);
        }

        this.jokerHelper.lambdaUpdate(SysDict.class)
                .saveBatch(sysDictList, 2);

        return "";
    }


    @GetMapping(value = "update")
    public String update() {

        SysDict sysDict = new SysDict();
        sysDict.setId(1900118886734848000L);
        sysDict.setDictKey("user_type")
                .setDictSort(1)
                .setDictTitle("test")
        ;
//        this.jokerHelper.saveOrUpdate(SysDict.class, sysDict);


        UpdateWrapper<SysDict> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SysDict::getId, 1900118886734848000L);
        this.jokerHelper.update(SysDict.class, updateWrapper, sysDict);


        return "";
    }

    @GetMapping(value = "updateList")
    public String updateList() {


        List<SysDict> sysDictList = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 2; i++) {
            SysDict sysDict = new SysDict();
            if (i == 0) {
                sysDict.setId(1900149302585913344L);
            }
            sysDict.setDictKey("user_type")
                    .setDictValue(1)
                    .setDictSort(1)
                    .setDictTitle("test");
            sysDictList.add(sysDict);
        }

        this.jokerHelper.lambdaUpdate(SysDict.class)
                .saveOrUpdateBatch(sysDictList);

        return "";
    }


    @GetMapping(value = "remove")
    public String remove() {

        SysDict sysDict = new SysDict();
        sysDict.setId(1900118886734848000L);
        sysDict.setDictKey("user_type")
                .setDictSort(1)
                .setDictTitle("test")
        ;
        this.jokerHelper.lambdaUpdate(SysDict.class)
                .eq(SysDict::getDictValue, 1)
                .remove();

        return "";
    }

}
